#include"Connection.h"
#include"log.hpp"

//bool read()
//{
//	FILE* pf = fopen("./mysql.conf", "r");
//	if (pf == nullptr)
//	{
//		LOG(ERROR, "MySQL配置文件打开失败");
//		return false;
//	}
//
//	LOG(ERROR, "MySQL配置文件打开成功");
//	while (!feof(pf)) //没有读取到文件结尾就继续读
//	{
//		char buf[1024] = { 0 };
//		fgets(buf, sizeof(buf) - 1, pf); //一次读取一行配置信息 => port=3306\n
//		buf[strlen(buf) - 1] = '\0';//将最后的换行符替换成\0
//		
//		string ret = buf;
//		//以等号(=)区分每一个配置项的key和value
//		size_t index = ret.find('=');
//		if (index == std::string::npos) //说明当前配置项无效=>可能是注释
//		{
//			continue; 
//		}
//
//		//index位置为空格   [0,index) => key  [index+1,...] =>value
//		string key = ret.substr(0, index);
//		string value = ret.substr(index + 1);
//		cout << key << "---" << value << endl;
//	}
//	LOG(NORMAL, "MySQL配置项读取成功");
//	return true;
//}
//
//
//int main()
//{
//	//read();
//	Connection con;
//	char sql[1024] = { 0 };
//	//sprintf(sql, "insert into user(name,age,sex) values('%s',%d,'%s')", "Mango", 20, "male");
//	sprintf(sql, "select * from user");
//	con.connect("127.0.0.1",3306,"Mango","xxx","chat");
//	con.query(sql);
//
//	return 0;
#include"Connection.h"
using namespace std;
#include"ConnectionPool.h"
void SigleWithConnection()
{
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		ConnectionPool* cp = ConnectionPool::GetInstance();

		shared_ptr<Connection> sp = cp->GetConnection();
		char sql[1024] = { 0 };
		sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
			"zhangsan", 20, "male");
		sp->update(sql);
	}
	time_t end = clock();
	cout << end - begin << endl;
}
void SigleWithConnection()
{
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		ConnectionPool* cp = ConnectionPool::GetInstance();

		shared_ptr<Connection> sp = cp->GetConnection();
		char sql[1024] = { 0 };
		sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
			"zhangsan", 20, "male");
		sp->update(sql);
		/*string sql = "select * from user";
		sp->query(sql);*/
	}
	time_t end = clock();
	cout << end - begin << endl;
}

void MutiWithConnection()
{
	time_t begin = clock();
	thread t1([]() {
		for (int i = 0; i < 2500; ++i)
		{
			ConnectionPool* cp = ConnectionPool::GetInstance();

			shared_ptr<Connection> sp = cp->GetConnection();
			char sql[1024] = { 0 };
			sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
				"zhangsan", 20, "male");
			if (sp == nullptr)
			{
				cout << "sp is empty" << endl;
				continue;
			}
			sp->update(sql);
		}
		});
	thread t2([]() {
		for (int i = 0; i < 2500; ++i)
		{
			ConnectionPool* cp = ConnectionPool::GetInstance();

			shared_ptr<Connection> sp = cp->GetConnection();
			char sql[1024] = { 0 };
			sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
				"zhangsan", 20, "male");
			if (sp == nullptr)
			{
				cout << "sp is empty" << endl;
				continue;
			}
			sp->update(sql);
		}
		});
	thread t3([]() {
		for (int i = 0; i < 2500; ++i)
		{
			ConnectionPool* cp = ConnectionPool::GetInstance();

			shared_ptr<Connection> sp = cp->GetConnection();
			char sql[1024] = { 0 };
			sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
				"zhangsan", 20, "male");
			if (sp == nullptr)
			{
				cout << "sp is empty" << endl;
				continue;
			}
			sp->update(sql);
		}
		});
	thread t4([]() {
		for (int i = 0; i < 2500; ++i)
		{
			ConnectionPool* cp = ConnectionPool::GetInstance();

			shared_ptr<Connection> sp = cp->GetConnection();
			char sql[1024] = { 0 };
			sprintf(sql, "insert into user(name,age,sex) values('%s','%d','%s')",
				"zhangsan", 20, "male");
			if (sp == nullptr)
			{
				cout << "sp is empty" << endl;
				continue;
			}
			sp->update(sql);
		}
		});

	t1.join();
	t2.join();
	t3.join();
	t4.join();
	time_t end = clock();
	cout << end - begin << endl;
}
int main()
{
	//MutiWithConnection();
	SigleWithConnection();
	return 0;
}
